package com.aabte.httpclient;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

public class MyCookiesForPost {

    private String baseUrl;
    private ResourceBundle bundle;
    private CookieStore store = new BasicCookieStore();

    @BeforeTest
    public void beforeTest() {
        bundle = ResourceBundle.getBundle("application", Locale.CHINA);
    }

    @BeforeTest(dependsOnMethods = "beforeTest")
    public void setUrl() {
        baseUrl = bundle.getString("test.baseurl");
    }

    @Test
    public void testGetCookies() throws IOException {
        String result;
        // 拼接测试url
        String uri = bundle.getString("test.getCookies.uri");
        String testUrl = this.baseUrl + uri;
        HttpGet get = new HttpGet(testUrl);
        HttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();
        HttpResponse httpResponse = client.execute(get);
        result = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
        System.out.println(httpResponse.getStatusLine().getStatusCode());
        System.out.println(result);

        List<Cookie> cookies = store.getCookies();
        System.out.println(cookies.size());
        for (Cookie cookie :
                cookies) {
            System.out.println(cookie.getName() + " = " + cookie.getValue());
        }
    }


    @Test(dependsOnMethods = "testGetCookies")
    public void testPostMethod() throws IOException {
        String uri = bundle.getString("test.postWithCookies.uri");
        String testUrl = baseUrl + uri;

        // 创建post对象
        HttpPost post = new HttpPost(testUrl);
        // 设置请求头信息
        post.addHeader("Content-Type", "application/json");
        // 设置请求体
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "huhansan");
        jsonObject.put("age", 18);
        StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
        post.setEntity(entity);
        // 创建客户端对象
        HttpClient client = HttpClients.custom().setDefaultCookieStore(store).build();

        // 执行请求，获取结果
        HttpResponse httpResponse = client.execute(post);

        System.out.println(httpResponse.getStatusLine().getStatusCode());

        // 处理结果
        String result = EntityUtils.toString(httpResponse.getEntity());
        JSONObject resultObject = new JSONObject(result);
        String msg = resultObject.getString("msg");
        int status = resultObject.getInt("status");
        Assert.assertEquals("success", msg);
        Assert.assertEquals(1, status);

    }

}
